package com.behome.netty.netty_A.server;

import com.behome.netty.netty_A.bean.ProtocolAPacket;
import com.behome.netty.netty_A.handler.BusinessAHandlerBase;
import com.behome.netty.netty_A.handler.BusinessAHandlerFactory;
import io.netty.channel.*;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

@Slf4j
public class StandardAPrivateServerHandler extends SimpleChannelInboundHandler<ProtocolAPacket> {

    private static final String businessHandlerPackagePath = "com.behome.netty.netty_A.handler.A50Handler";

    /**
     * 有客户端连接服务器会触发此函数
     *
     * @param ctx ctx
     * @author Ljy
     * @date 2021/3/10 15:00
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = insocket.getAddress().getHostAddress();
        //获取连接通道唯一标识
        ChannelId channelId = ctx.channel().id();
//        log.info("客户端【" + channelId + "】连接指令服务器[IP:" + clientIp + "--->PORT:" + insocket.getPort() + "]");
    }

    /**
     * 有客户端终止连接服务器会触发此函数
     *
     * @param ctx ctx
     * @author Ljy
     * @date 2021/3/10 15:01
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = insocket.getAddress().getHostAddress();
        ChannelId channelId = ctx.channel().id();
//        log.info("客户端【" + channelId + "】退出指令服务器[IP:" + clientIp + "--->PORT:" + insocket.getPort() + "]");
    }

    /**
     * @description:
     * @param: null
     * @return:
     * @author bh_ljy
     * @date: 2024/3/28 10:01
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ProtocolAPacket msg) {
        log.debug("收到消息:{}", msg);
        BusinessAHandlerBase businessHandler = BusinessAHandlerFactory.createBusinessHandler(ctx, msg, businessHandlerPackagePath,0);
        if (businessHandler != null) {
            try {
                businessHandler.resolve();
            } catch (Exception var8) {
                log.error("适配器发给平台的数据在处理时发生错误.", var8);
            }
        } else {
            log.error("适配器发给平台的数据在处理时发生错误.未有正确的处理器来处理该数据");
        }
    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        String socketString = ctx.channel().remoteAddress().toString();
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) {
                log.info("Client: " + socketString + " READER_IDLE 读超时");
            } else if (event.state() == IdleState.WRITER_IDLE) {
                log.info("Client: " + socketString + " WRITER_IDLE 写超时");
            } else if (event.state() == IdleState.ALL_IDLE) {
                log.info("Client: " + socketString + " ALL_IDLE 总超时");
            }
        }
    }

    /**
     * 发生异常会触发此函数
     *
     * @param ctx   通道
     * @param cause cause
     * @author Ljy
     * @date 2021/3/10 15:02
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
        log.warn(ctx.channel().id() + " 发生了错误,此连接被关闭");
        cause.printStackTrace();
    }

}
